#ifndef CARDMANAGE_H
#define CARDMANAGE_H

#include "character.h"
#include "player.h"

class Compare
{
public:
	bool operator() (int f, int s)
	{
		CCard *first = pm->Player[f].CharSelf;
		CCard *second = pm->Player[s].CharSelf;
		int firstfind = -1;
		int secondfind = -1;
		int i = 0;
		for (CCardSet::iterator it = set->begin(); it != set->end(); it++)
		{
			if ( *it == first )
				firstfind = i;
			if ( *it == second )
				secondfind = i;
			i++;
		}
		if (firstfind < secondfind) return true;
		return false;
	}
	Compare(CCardSet *s, CPlayerManage *p) : set(s), pm(p) {}
private:
	CCardSet *set;
	CPlayerManage *pm;
};


class CCardManage
{
public:

	CCardSet CharactersUsing;
	CCardSet NavigationsUsing;
	CCardSet ItemsUsing;
	CCardSet Position;

	CCardSet CharactersChosen;
	CCardSet NavigationsStack;
	CCardSet NavigationsUsed;
	CCardSet ItemsStack;
	CCardSet ItemsUsed;

public:
	void Init(const CCardSet &chars, const CCardSet &navis, const CCardSet &items, const CCardSet &pos)
	{
		// card stack init
		CharactersUsing = chars;
		NavigationsUsing = navis;
		ItemsUsing = items;
		Position = pos;
		ItemsStack = ItemsUsing;
		ItemsStack.Wash();
		NavigationsStack = NavigationsUsing;
		NavigationsStack.Wash();
	}
	void PlayerAlloc(CPlayerManage *pm, int number)
	{
		CPlayer *player = pm->Player;
		// init self hate & love
		CharactersChosen = CharactersUsing.RandChoose(number);
		for (int i = 0; i < number; i++)
		{
			player[i].CharLove = static_cast<CCharacter*>( *CharactersChosen[i] );
		}
		CharactersChosen.Wash();
		for (int i = 0; i < number; i++)
		{
			player[i].CharHate = static_cast<CCharacter*>( *CharactersChosen[i] );
		}
		CharactersChosen.Wash();
		for (int i = 0; i < number; i++)
		{
			player[i].CharSelf = static_cast<CCharacter*>( *CharactersChosen[i] );
		}
		// init position
		Position.remove_if( NotInList(&CharactersChosen) );
		for (int i = 0; i < number; i++)
		{
			pm->LastPosition.push_front(i);
		}
		pm->LastPosition.sort( Compare(&Position, pm) );
		pm->CurrentPosition = pm->LastPosition;

		// init first item
		CCardSet item;
		item = ItemsStack.PopFrontTo(number);
		for (int i = 0; i < number; i++)
		{
			player[i].Inventory.push_front(*item[i]);
		}
		ItemsUsed.splice(ItemsUsed.begin(), item);
	}

	CCardSet GetNumberItems(int number)
	{
		CCardSet item;
		item = ItemsStack.PopFrontTo(number);
		ItemsUsed.splice(ItemsUsed.begin(), item);
	}

	//void CardStackInit(CBasicGameCard basic);

};

















#endif